/*
 * @(#)OfflineSqlTableDaoImpl.java   1.0  2021年11月17日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.swift.dts.dao.OfflineSqlTableDao;
import com.swift.dts.dao.model.OfflineSqlTable;
import com.swift.dts.dao.model.SynObjectKey;
import com.swift.dts.db.LocalDbDataSource;
import com.swift.dts.util.CreateExecSql;
import com.swift.util.type.JsonUtil;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明
 * 
 * @author zhengjiajin
 * @version 1.0 2021年11月17日
 */
@Component
public class OfflineSqlTableDaoImpl implements OfflineSqlTableDao {

    @Autowired
    private LocalDbDataSource localDbDataSource;

    /**
     * @see com.swift.dts.dao.OfflineSqlTableDao#save(com.swift.dts.dao.model.OfflineSqlTable)
     */
    @Override
    public void save(OfflineSqlTable offlineSqlTable) {
        String sql = "INSERT INTO offline_sql_table(destination,data_source,db_name,table_name,pk_key,sql_str,create_time)  VALUES('"
            + offlineSqlTable.getDestination() + "','" + offlineSqlTable.getDataSource() + "','"
            + offlineSqlTable.getDbName() + "','" + offlineSqlTable.getTableName() + "','" + offlineSqlTable.getPkKey()
            + "','" + CreateExecSql.formatValue(offlineSqlTable.getSqlStr()) + "','" + offlineSqlTable.getCreateTime()
            + "')";
        localDbDataSource.execute(sql);
    }

    /**
     * @see com.swift.dts.dao.OfflineSqlTableDao#delete(java.lang.Long)
     */
    @Override
    public void delete(Long sqlId) {
        String sql = "DELETE FROM offline_sql_table WHERE sql_id=" + sqlId;
        localDbDataSource.execute(sql);
    }

    /**
     * @see com.swift.dts.dao.OfflineSqlTableDao#list(com.swift.dts.dao.model.SynObjectKey)
     */
    @Override
    public List<OfflineSqlTable> list(SynObjectKey synObjectKey) {
        String sql = "SELECT * FROM offline_sql_table " + where(synObjectKey) + " ORDER BY sql_id ";
        List<Map<String, Object>> list = localDbDataSource.queryForList(sql);
        if (TypeUtil.isNull(list)) return null;
        return JsonUtil.toListObject(JsonUtil.toJson(list), OfflineSqlTable.class);
    }

    private String where(SynObjectKey synObjectKey) {
        if (synObjectKey == null) return " ";
        StringBuffer sb = new StringBuffer();
        if (TypeUtil.isNotNull(synObjectKey.getDestination())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" destination='" + synObjectKey.getDestination() + "' ");
        }
        if (TypeUtil.isNotNull(synObjectKey.getDataSource())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" data_source='" + synObjectKey.getDataSource() + "' ");
        }
        if (TypeUtil.isNotNull(synObjectKey.getDbName())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" db_name='" + synObjectKey.getDbName() + "' ");
        }
        if (TypeUtil.isNotNull(synObjectKey.getTableName())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" table_name='" + synObjectKey.getTableName() + "' ");
        }
        if (sb.length() <= 0) return " ";
        return " WHERE " + sb.toString();
    }

    /** 
     * @see com.swift.dts.dao.OfflineSqlTableDao#delete(com.swift.dts.dao.model.SynObjectKey)
     */
    @Override
    public void delete(SynObjectKey synObjectKey) {
        String sql = "DELETE FROM offline_sql_table " + where(synObjectKey);
        localDbDataSource.execute(sql);
    }

    /** 
     * @see com.swift.dts.dao.OfflineSqlTableDao#list()
     */
    @Override
    public List<OfflineSqlTable> list() {
        String sql = "SELECT * FROM offline_sql_table ORDER BY sql_id OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY";
        List<Map<String, Object>> list = localDbDataSource.queryForList(sql);
        if (TypeUtil.isNull(list)) return null;
        return JsonUtil.toListObject(JsonUtil.toJson(list), OfflineSqlTable.class);
    }

}
